package com.lear.njava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lear.njava.mapper.GradeMapper;
import com.lear.njava.pojo.Grade;
import com.lear.njava.service.GradeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {
    @Override
    public IPage<Grade> getGradeByOpr(Page<Grade> page, String gradeName) {
        // 创建查询包裹对象,拼接查询条件
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<Grade>();
        // 判断gradeName是否为空
        if (!StringUtils.isEmpty(gradeName)) {
            //gradeName不为空，进行模糊匹配
            queryWrapper.like("name", gradeName);
        }
        // 根据id降序排序
        queryWrapper.orderByDesc("id");
        queryWrapper.orderByAsc("name");
        // 创建baseMapper传入拼接好的queryWrapper查询条件进行查询
        Page<Grade> gradePage = baseMapper.selectPage(page, queryWrapper);
        return gradePage;
    }

    @Override
    public List<Grade> getGrades() {
        List<Grade> grades = baseMapper.selectList(null); // 查询全部年级信息
        return grades;
    }
}
